package com.burgeon.backend.SHOPBACKEND.rest.openCardTask;

import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.SHOPBACKEND.entity.openCardTask.VipListViewData;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;


public class VipListView extends RESTBase {

    private final Integer NUM = 10;

    private String ip;
    private String account;
    private String password;

    public VipListView(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }


    /**
     * 发送并接收字符创
     * @return
     */

    public ArrayList<VipListViewData> getData(Integer start) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";
        String str = RESULT(Data(start), url);
        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception(str);
        }
        String count = new JSONArray(str).getJSONObject(0).getString("count");
        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");

        ArrayList<VipListViewData> vipList = new ArrayList<VipListViewData>();

        for (int i = 0; i < jsonArray.length(); i++) {


            VipListViewData vip = new VipListViewData();
            vip.setId(jsonArray.getJSONArray(i).getString(0));
            vip.setEmpName(jsonArray.getJSONArray(i).getString(1));
            vip.setStore(jsonArray.getJSONArray(i).getString(2));
            vip.setVipName(jsonArray.getJSONArray(i).getString(3));
            vip.setVipPhone(jsonArray.getJSONArray(i).getString(4));
            vip.setOpenCardDate(jsonArray.getJSONArray(i).getString(5));
            vip.setEmpId(jsonArray.getJSONArray(i).getString(6));
            vip.setCount(count);
            vipList.add(vip);
        }
        return vipList;
    }

    private MultiValueMap Data(Integer start) {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段

        columns.add("ID");
        columns.add("'HR_EMPLOYEE_ID;NAME'");
        columns.add("'C_STORE_ID;NAME'");
        columns.add("VIPNAME");
        columns.add("CARDNO");
        columns.add("OPENCARDDATE");
        columns.add("HR_EMPLOYEE_ID");

        params.put("columns", columns);
        params.put("table", "C_VIP");//表名

        //设置查询规则（从第几条查询，每页显示多少条数）
        params.put("range", NUM);
        params.put("count", "true");
        params.put("start", start*NUM);

        //设置排序规则
        HashMap orderby = new HashMap();
        orderby.put("column", "ID");
        orderby.put("asc", "false");
        ArrayList list = new ArrayList();
        list.add(orderby);

        params.put("orderby", list);


        return DATA("Query", params,account,password);
    }

    public static void main(String[] args) {
        VipListView querryYCRetailout = new VipListView("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        try {
            System.err.println(querryYCRetailout.getData(1));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
